package com.top.ruizhong.mode.api

import com.module.lemlin.mode.SubscribeListener
import com.module.lemlin.rxhttp.CustomException
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.subscribe
import com.top.ruizhong.entity.*
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.RxHttp


/**
 * 发送短信验证码
 *
 * @param mobile 手机号
 * @param event 事件：login,set_pwd
 * */
fun userSmsSend(
    mobile: String, event: String, listener: SubscribeListener<UserSmsResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/sms/send")
        .setAssemblyEnabled(false)
        .add("mobile", mobile)
        .add("event", event)
        .asClass(UserSmsResponse::class.java)
    , listener
)

/**
 * 短信验证码登录
 *
 * @param mobile 手机号
 * @param smsCode 短信验证码
 * */
fun userLogin(
    mobile: String, smsCode: String, listener: SubscribeListener<UserLoginResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/user/login")
        .setAssemblyEnabled(false)
        .add("mobile", mobile)
        .add("sms_code", smsCode)
        .asClass(UserLoginResponse::class.java)
    , listener
)

/**
 * 个人信息
 * */
fun userMe(listener: SubscribeListener<UserMeResponse>): Disposable = subscribe(
    RxHttp.postForm("/api/user/me")
        .asClass(UserMeResponse::class.java)
    , listener
)

/**
 * 关注
 *
 * @param userId
 * */
fun userFollow(
    userId: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/follow_list")
        .add("user_id", userId)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 取消关注
 *
 * @param userId
 * */
fun userUnFollow(
    userId: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/follow_list")
        .add("user_id", userId)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 关注列表
 *
 * @param page 页数
 * @param limit 条数
 * */
fun userFollowList(
    page: String, limit: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.get("/api/user/follow_list")
        .add("page", page)
        .add("limit", limit)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 粉丝列表
 *
 * @param page 页数
 * @param limit 条数
 * */
fun userFansList(
    page: String, limit: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.get("/api/user/fans_list")
        .add("page", page)
        .add("limit", limit)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 互相关注列表
 *
 * @param page 页数
 * @param limit 条数
 * */
fun userMutualFollowList(
    page: String, limit: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.get("/api/user/mutual_follow_list")
        .add("page", page)
        .add("limit", limit)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 作品列表
 *
 * @param userId
 * @param page 页数
 * @param limit 条数
 * */
fun userVideos(
    userId: String, page: String, limit: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.get("/api/user/videos")
        .add("user_id", userId)
        .add("page", page)
        .add("limit", limit)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 喜欢的作品列表
 *
 * @param userId
 * @param page 页数
 * @param limit 条数
 * */
fun userLikeVideos(
    userId: String, page: String, limit: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.get("/api/user/like_videos")
        .add("user_id", userId)
        .add("page", page)
        .add("limit", limit)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 我的作品 / 草稿箱
 *
 * @param status 状态 0草稿箱,1待审核,2审核通过,3:审核拒绝
 * @param page 页数
 * @param limit 条数
 * */
fun userMyVideos(
    status: String, page: String, limit: String, listener: SubscribeListener<VideoDraftResponse>
) = subscribe(
    RxHttp.get("/api/user/my_videos")
        .add("status", status)
        .add("page", page)
        .add("limit", limit)
        .asClass(VideoDraftResponse::class.java)
    , listener
)

/**
 * 设置访问密码
 *
 * @param smsCode 短信验证码
 * @param password 密码
 * @param confirmPassword 确认密码
 * */
fun userSetPwd(
    smsCode: String,
    password: String,
    confirmPassword: String,
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/set_pwd")
        .add("sms_code", smsCode)
        .add("password", password)
        .add("confirm_password", confirmPassword)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 私人草稿箱视频
 *
 * @param password 访问密码
 * @param page 页数
 * @param limit 条数
 * */
fun userPrivateDraftVideos(
    password: String, page: String, limit: String, listener: SubscribeListener<VideoDraftResponse>
) = subscribe(
    RxHttp.get("/api/user/private_draft_videos")
        .add("password", password)
        .add("page", page)
        .add("limit", limit)
        .asClass(VideoDraftResponse::class.java)
    , listener
)

//fun userPrivateDraftVideosIsPwd(
//    password: String, page: String, limit: String,
//    listener: SubscribeListener<VideoDraftResponse>
//): Disposable = subscribe(
//    RxHttp.postForm("/api/user/me")
//        .asClass(UserMeResponse::class.java)
//        .doOnNext {
//            if (it.data.password_set != 1) throw CustomException("8888", "密码未设置")
//        }
//        .flatMap {
//            RxHttp.get("/api/user/private_draft_videos")
//                .add("password", password)
//                .add("page", page)
//                .add("limit", limit)
//                .asClass(VideoDraftResponse::class.java)
//        }
//    , listener
//)

/**
 * 修改用户信息
 *
 * @param avatar 头像
 * @param nickname 昵称
 * @param introduction 简介
 * */
fun userProfile(
    avatar: String, nickname: String, introduction: String,
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/profile")
        .add("avatar", avatar)
        .add("nickname", nickname)
        .add("introduction", introduction)
        .asClass(HttpServiceData::class.java)
    , listener
)

/**
 * 获取数据总览
 * */
fun userStat(
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/stat")
        .asClass(HttpServiceData::class.java)
    , listener
)

